package cn.biao.epic.sys.modular.system.service;

import cn.biao.epic.base.enmus.CommonStatus;
import cn.biao.epic.core.util.ToolUtil;
import cn.biao.epic.model.exception.RequestEmptyException;
import cn.biao.epic.model.exception.ServiceException;
import cn.biao.epic.sys.core.exception.enums.BizExceptionEnum;
import cn.biao.epic.sys.modular.system.entity.Dict;
import cn.biao.epic.sys.modular.system.entity.DictType;
import cn.biao.epic.sys.modular.system.mapper.DictTypeMapper;
import cn.biao.epic.sys.modular.system.model.params.DictTypeParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @Auther Uncle_Z
 * @date 2019/12/6 18:42
 * @Email:2545375@qq.com
 * @Descrition
 */
@Service
public class DictTypeService extends ServiceImpl<DictTypeMapper, DictType> {

    @Autowired
    private DictService dictService;

    /**
     * 
     * 新增
     *
     * @author uncle_z
     * @Date 2019/12/7 14:42
     * @Param [param]
     * @File DictTypeService.java 
     */
    public void add(DictTypeParam param) {
        LambdaQueryWrapper<DictType> dictTypeLambdaQueryWrapper = new QueryWrapper<DictType>().lambda();
        dictTypeLambdaQueryWrapper.eq(DictType::getCode,param.getCode()).or().eq(DictType::getName,param.getName());
        List<DictType> list =this.list(dictTypeLambdaQueryWrapper);
        if (list != null && list.size() >0) {
            throw new ServiceException(BizExceptionEnum.DICT_EXISTED);
        }
        DictType entity = getEntity(param);
        entity.setStatus(CommonStatus.ENABLE.getCode());
        this.save(entity);
    }


    /**
     *
     * 删除
     *
     * @author uncle_z
     * @Date 2019/12/8 14:59
     * @Param [param]
     * @File DictTypeService.java
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(DictTypeParam param) {
        if (param ==null || param.getDictTypeId() == null) {
            throw new RequestEmptyException("字典类型ID为空");
        }
        //删除字典类型
        this.removeById(getKey(param));

        //删除字典
        this.dictService.remove(new QueryWrapper<Dict>().eq("dict_type_id",getKey(param)));
    }

    /**
     * 
     * 更新
     *
     * @author uncle_z
     * @Date 2019/12/8 15:00
     * @Param [param]
     * @File DictTypeService.java 
     */
    public void update(DictTypeParam param){
        DictType oldEntity = getOldEntity(param);
        DictType newEntity = getEntity(param);

        ToolUtil.copyProperties(newEntity, oldEntity);
        QueryWrapper<DictType> wrapper = new QueryWrapper<DictType>()
                .and(i -> i.eq("code",newEntity.getCode()).or().eq("name",newEntity.getName()))
                .and(i ->i.ne("dict_type_id",newEntity.getDictTypeId()));
        int dicts = this.count(wrapper);
        if(dicts >0){
            throw new ServiceException(BizExceptionEnum.DICT_EXISTED);
        }

        this.updateById(newEntity);
    }

    /**
     *
     * 分页查询数据
     *
     * @author uncle_z
     * @Date 2019/12/8 16:01
     * @Param [param]
     * @File DictTypeService.java
     */
    public IPage<DictType> findPageBySpec(DictTypeParam param){
        Page<DictType> dictTypePage = new Page<>(1,20);
        QueryWrapper<DictType> dictTypeQueryWrapper = new QueryWrapper<>();
        if (ToolUtil.isNotEmpty(param.getCondition())) {
            dictTypeQueryWrapper.and(i->i.eq("code",param.getCondition())
                    .or().eq("name",param.getCondition()));
        }
        if (ToolUtil.isNotEmpty(param.getStatus())) {
            dictTypeQueryWrapper.and(i -> i.eq("status", param.getStatus()));
        }
        if (ToolUtil.isNotEmpty(param.getSystemFlag())) {
            dictTypeQueryWrapper.and(i->i.eq("system_flag",param.getSystemFlag()));
        }
        dictTypeQueryWrapper.orderByAsc("dict_type_id");
        return this.page(dictTypePage,dictTypeQueryWrapper);
    }

    private Serializable getKey(DictTypeParam param){
        return param.getDictTypeId();
    }

    private DictType getOldEntity(DictTypeParam param){
        return this.getById(getKey(param));
    }

    private DictType getEntity(DictTypeParam param) {
        DictType entity = new DictType();
        ToolUtil.copyProperties(param,entity);
        return entity;
    }
}
